<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();

function geocodePosition(pos) {
  geocoder.geocode({
    latLng: pos
  }, function(responses) {
    if (responses && responses.length > 0) {
      updateMarkerAddress(responses[0].formatted_address);
    } else {
      updateMarkerAddress('Cannot determine address at this location.');
    }
  });
}


function updateMarkerPositionLat(latLng) {
  document.getElementById('lat').value = [
    latLng.lat()
  ];
}

function updateMarkerPositionLng(latLng) {
	  document.getElementById('lng').value = [
	    latLng.lng()
	  ];
	}

function initialize() {
  var latLng = new google.maps.LatLng(<?php if($model->lat != null)echo $model->lat; else echo '40.735546913188955';?>, 
  										<?php if($model->lng != null)echo $model->lng; else echo '-74.0107421875';?>);
  var map = new google.maps.Map(document.getElementById('mapCanvas'), {
    zoom: 8,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });
  var marker = new google.maps.Marker({
    position: latLng,
    title: 'Point A',
    map: map,
    draggable: true
  });
  
  // Update current position info.
  updateMarkerPositionLat(latLng);
  updateMarkerPositionLng(latLng);
  geocodePosition(latLng);
  
  // Add dragging event listeners.
  google.maps.event.addListener(marker, 'dragstart', function() {
    updateMarkerAddress('');
  });
  
  google.maps.event.addListener(marker, 'drag', function() {
    updateMarkerPositionLat(marker.getPosition());
    updateMarkerPositionLng(marker.getPosition());
  });
  
  google.maps.event.addListener(marker, 'dragend', function() {
    geocodePosition(marker.getPosition());
  });
}

// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
</script>

<style>
#mapCanvas {
	width: 500px;
	height: 400px;
}

#infoPanel {
	
}
</style>

<div class="widget-box">
<div class="widget-header widget-header-blue widget-header-flat">
	<h4 class="lighter"><?php echo $model->isNewRecord ? 'Create Business' : 'Update Business'?></h4>
</div>
<div class="widget-body" style="padding-top: 20px;">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'business-form',
	'enableAjaxValidation'=>false,
	'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>

	<?php echo $form->errorSummary($model, null, null, array('class'=>'alert alert-error')); ?>

	<div class="control-group">
		<?php echo $form->labelEx($model,'name', array('class'=>'control-label')); ?>
		<div class="controls">
			<?php echo $form->textField($model,'name'); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'address', array('class'=>'control-label')); ?>
		<div class="controls">
			<?php echo $form->textField($model,'address',array('size'=>60,'maxlength'=>255)); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'state_id', array('class'=>'control-label')); ?>
		<div class="controls">
			<?php echo $form->dropDownList($model,'state_id', CHtml::listData(States::model()->findAll(), 'id', 'name')); ?>
		</div>
	</div>
	
	<div class="control-group">
		<label class="control-label">Location </label>
		<div class="controls">
			<div id="mapCanvas"></div>
			<div id="infoPanel">
				<?php echo $form->textField($model,'lat',array('size'=>60,'maxlength'=>255, 'class'=>'form-control', 'id'=>'lat')); ?>
				<?php echo $form->textField($model,'lng',array('size'=>60,'maxlength'=>255, 'class'=>'form-control', 'id'=>'lng')); ?>
			</div>
		</div>
	</div>


	<div class="control-group">
		<?php echo $form->labelEx($model,'website', array('class'=>'control-label')); ?>
		<div class="controls">
			<?php echo $form->textField($model,'website',array('size'=>60,'maxlength'=>255)); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'phone', array('class'=>'control-label')); ?>
		<div class="controls">
			<?php echo $form->textField($model,'phone', array('size'=>60,'maxlength'=>255)); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'opening_hour', array('class'=>'control-label')); ?>
		<div class="controls">
			<?php echo $form->textField($model,'opening_hour',array('class'=>'business_input')); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'status', array('class'=>'control-label')); ?>
		<div class="controls">
			<?php echo $form->dropDownList($model,'status_ready', array('0'=>'Disabled', '1'=>'Ready', '3'=>'Duplicate', '4'=>'Blocked'), $model->isNewRecord ? array('options' => array('1'=>array('selected'=>true))) : ''); ?>
		</div>
	</div>

	<div class="form-actions" style="background-color: white;">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class'=>'btn btn-primary')); ?>
	</div>

<?php $this->endWidget(); ?>

</div>
</div><!-- form -->